<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>类型声明 | 清风随笔</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="/docs/assets/img/favicon.ico">
    <meta name="description" content="清风随笔">
    <meta rel="author" content="清风随笔">
    <meta rel="keywords" content="前端笔记，清风随笔">
    <link rel="preload" href="/docs/assets/css/0.styles.444b3866.css" as="style"><link rel="preload" href="/docs/assets/js/app.9a1ea3d7.js" as="script"><link rel="preload" href="/docs/assets/js/2.b157a060.js" as="script"><link rel="preload" href="/docs/assets/js/9.1484fc35.js" as="script"><link rel="prefetch" href="/docs/assets/js/10.22d7ac9e.js"><link rel="prefetch" href="/docs/assets/js/11.213666d1.js"><link rel="prefetch" href="/docs/assets/js/12.c2bc9913.js"><link rel="prefetch" href="/docs/assets/js/13.b6f5507a.js"><link rel="prefetch" href="/docs/assets/js/14.691b0567.js"><link rel="prefetch" href="/docs/assets/js/15.6390a7cc.js"><link rel="prefetch" href="/docs/assets/js/16.87305a0e.js"><link rel="prefetch" href="/docs/assets/js/17.72be41e6.js"><link rel="prefetch" href="/docs/assets/js/18.abea1bb2.js"><link rel="prefetch" href="/docs/assets/js/19.6d43f402.js"><link rel="prefetch" href="/docs/assets/js/20.5a22e6b1.js"><link rel="prefetch" href="/docs/assets/js/21.3ed6dff5.js"><link rel="prefetch" href="/docs/assets/js/22.28f18616.js"><link rel="prefetch" href="/docs/assets/js/23.ebe08f15.js"><link rel="prefetch" href="/docs/assets/js/24.1ec03719.js"><link rel="prefetch" href="/docs/assets/js/3.4bb508f8.js"><link rel="prefetch" href="/docs/assets/js/4.19f0a32d.js"><link rel="prefetch" href="/docs/assets/js/5.f96a32c3.js"><link rel="prefetch" href="/docs/assets/js/6.b5f8c138.js"><link rel="prefetch" href="/docs/assets/js/7.fde78f5d.js"><link rel="prefetch" href="/docs/assets/js/8.9feb6e45.js">
    <link rel="stylesheet" href="/docs/assets/css/0.styles.444b3866.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/docs/" class="home-link router-link-active"><img src="/docs/assets/img/logo.png" alt="清风随笔" class="logo"> <span class="site-name can-hide">清风随笔</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/docs/" class="nav-link">主页</a></div><div class="nav-item"><a href="/docs/pages/c3cd55/" class="nav-link">Vue</a></div><div class="nav-item"><a href="/docs/pages/2b5650/" class="nav-link">JavaScript</a></div><div class="nav-item"><a href="/docs/pages/11e8fa/" class="nav-link">TypeScript</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/docs/" class="nav-link">主页</a></div><div class="nav-item"><a href="/docs/pages/c3cd55/" class="nav-link">Vue</a></div><div class="nav-item"><a href="/docs/pages/2b5650/" class="nav-link">JavaScript</a></div><div class="nav-item"><a href="/docs/pages/11e8fa/" class="nav-link">TypeScript</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>TypeScript基础</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/pages/f09cf0/" class="sidebar-link">TypeScript介绍</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/docs/pages/f09cf0/#介绍" class="sidebar-link">介绍</a></li><li class="sidebar-sub-header"><a href="/docs/pages/f09cf0/#语言特性" class="sidebar-link">语言特性</a></li><li class="sidebar-sub-header"><a href="/docs/pages/f09cf0/#javascript-与-typescript-的区别" class="sidebar-link">JavaScript 与 TypeScript 的区别</a></li></ul></li><li><a href="/docs/pages/d2aa68/" class="sidebar-link">开发环境搭建</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/docs/pages/d2aa68/#typescript-开发环境搭建" class="sidebar-link">TypeScript 开发环境搭建</a></li></ul></li><li><a href="/docs/pages/12e874/" aria-current="page" class="active sidebar-link">类型声明</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/docs/pages/565233/" class="sidebar-link">编译选项</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/docs/pages/565233/#自动编译文件" class="sidebar-link">自动编译文件</a></li><li class="sidebar-sub-header"><a href="/docs/pages/565233/#自动编译整个项目" class="sidebar-link">自动编译整个项目</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/docs/pages/565233/#配置选项" class="sidebar-link">配置选项</a></li></ul></li></ul></li><li><a href="/docs/pages/978d74/" class="sidebar-link">webpack打包TypeScript</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/docs/pages/978d74/#webpack" class="sidebar-link">webpack</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/docs/pages/978d74/#步骤" class="sidebar-link">步骤</a></li></ul></li><li class="sidebar-sub-header"><a href="/docs/pages/978d74/#babel" class="sidebar-link">Babel</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/docs/pages/978d74/#步骤-2" class="sidebar-link">步骤</a></li></ul></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>TypeScript面向对象</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-33863c7e><div class="articleInfo" data-v-33863c7e><ul class="breadcrumbs" data-v-33863c7e><li data-v-33863c7e><a href="/docs/" title="首页" class="iconfont icon-home router-link-active" data-v-33863c7e></a></li> <li data-v-33863c7e><a href="/docs/categories/?category=TypeScript" title="分类" data-v-33863c7e>TypeScript</a></li> <li data-v-33863c7e><a href="/docs/categories/?category=TypeScript%E5%9F%BA%E7%A1%80" title="分类" data-v-33863c7e>TypeScript基础</a></li> <!----></ul> <div class="info" data-v-33863c7e><!----> <div title="创建时间" class="date iconfont icon-riqi" data-v-33863c7e><a href="javascript:;" data-v-33863c7e>2020-12-23</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-content"></div></div></div> <h1><img src="">
          类型声明
        </h1> <!----> <div class="theme-vdoing-content content__default"><h3 id="类型声明"><a href="#类型声明" class="header-anchor">#</a> 类型声明</h3> <ul><li>类型声明是 TS 非常重要的一个特点</li> <li>通过类型声明可以指定 TS 中变量（参数、形参）的类型</li> <li>指定类型后，当为变量赋值时，TS 编译器会自动检查值是否符合类型声明，符合则赋值，否则报错</li> <li>简而言之，类型声明给变量设置了类型，使得变量只能存储某种类型的值</li> <li>语法：</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> 变量<span class="token operator">:</span> 类型<span class="token punctuation">;</span>

<span class="token keyword">let</span> 变量<span class="token operator">:</span> 类型 <span class="token operator">=</span> 值<span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token parameter">参数<span class="token operator">:</span> 类型<span class="token punctuation">,</span> 参数<span class="token operator">:</span> 类型</span><span class="token punctuation">)</span><span class="token operator">:</span> 类型<span class="token punctuation">{</span>
    <span class="token operator">...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="自动类型判断"><a href="#自动类型判断" class="header-anchor">#</a> 自动类型判断</h3> <ul><li>TS 拥有自动的类型判断机制</li> <li>当对变量的声明和赋值是同时进行的，TS 编译器会自动判断变量的类型</li> <li>所以如果你的变量的声明和赋值时同时进行的，可以省略掉类型声明</li></ul> <h3 id="类型"><a href="#类型" class="header-anchor">#</a> 类型</h3> <table><thead><tr><th style="text-align:center;">类型</th> <th style="text-align:center;">例子</th> <th style="text-align:center;">描述</th></tr></thead> <tbody><tr><td style="text-align:center;">number</td> <td style="text-align:center;">1, -33, 2.5</td> <td style="text-align:center;">任意数字</td></tr> <tr><td style="text-align:center;">string</td> <td style="text-align:center;">'hi', &quot;hi&quot;, <code>hi</code></td> <td style="text-align:center;">任意字符串</td></tr> <tr><td style="text-align:center;">boolean</td> <td style="text-align:center;">true、false</td> <td style="text-align:center;">布尔值 true 或 false</td></tr> <tr><td style="text-align:center;">字面量</td> <td style="text-align:center;">其本身</td> <td style="text-align:center;">限制变量的值就是该字面量的值</td></tr> <tr><td style="text-align:center;">any</td> <td style="text-align:center;">*</td> <td style="text-align:center;">任意类型</td></tr> <tr><td style="text-align:center;">unknown</td> <td style="text-align:center;">*</td> <td style="text-align:center;">类型安全的 any</td></tr> <tr><td style="text-align:center;">void</td> <td style="text-align:center;">空值（undefined）</td> <td style="text-align:center;">没有值（或 undefined）</td></tr> <tr><td style="text-align:center;">never</td> <td style="text-align:center;">没有值</td> <td style="text-align:center;">不能是任何值</td></tr> <tr><td style="text-align:center;">object</td> <td style="text-align:center;">{name:'孙悟空'}</td> <td style="text-align:center;">任意的 JS 对象</td></tr> <tr><td style="text-align:center;">array</td> <td style="text-align:center;">[1,2,3]</td> <td style="text-align:center;">任意 JS 数组</td></tr> <tr><td style="text-align:center;">tuple</td> <td style="text-align:center;">[4,5]</td> <td style="text-align:center;">元素，TS 新增类型，固定长度数组</td></tr> <tr><td style="text-align:center;">enum</td> <td style="text-align:center;">enum{A, B}</td> <td style="text-align:center;">枚举，TS 中新增类型</td></tr></tbody></table> <h4 id="number"><a href="#number" class="header-anchor">#</a> number</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> decimal<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> hex<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">0xf00d</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> binary<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">0b1010</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> octal<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">0o744</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> big<span class="token operator">:</span> bigint <span class="token operator">=</span> <span class="token number">100n</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="boolean"><a href="#boolean" class="header-anchor">#</a> boolean</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> isDone<span class="token operator">:</span> <span class="token builtin">boolean</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="string"><a href="#string" class="header-anchor">#</a> string</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> color<span class="token operator">:</span> <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">;</span>
color <span class="token operator">=</span> <span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">let</span> fullName<span class="token operator">:</span> <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Bob Bobbington</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
<span class="token keyword">let</span> age<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">37</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> sentence<span class="token operator">:</span> <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Hello, my name is </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>fullName<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">.
 
 I'll be </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>age <span class="token operator">+</span> <span class="token number">1</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> years old next month.</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="字面量"><a href="#字面量" class="header-anchor">#</a> 字面量</h4> <ul><li>也可以使用字面量去指定变量的类型，通过字面量可以确定变量的取值范围</li></ul> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> color<span class="token operator">:</span> <span class="token string">&quot;red&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;blue&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;black&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> num<span class="token operator">:</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">2</span> <span class="token operator">|</span> <span class="token number">3</span> <span class="token operator">|</span> <span class="token number">4</span> <span class="token operator">|</span> <span class="token number">5</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="any"><a href="#any" class="header-anchor">#</a> any</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> d<span class="token operator">:</span> <span class="token builtin">any</span> <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span>
d <span class="token operator">=</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">;</span>
d <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="unknown"><a href="#unknown" class="header-anchor">#</a> unknown</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> notSure<span class="token operator">:</span> <span class="token builtin">unknown</span> <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span>
notSure <span class="token operator">=</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="void"><a href="#void" class="header-anchor">#</a> void</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> unusable<span class="token operator">:</span> <span class="token keyword">void</span> <span class="token operator">=</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="never"><a href="#never" class="header-anchor">#</a> never</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">function</span> <span class="token function">error</span><span class="token punctuation">(</span>message<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">never</span> <span class="token punctuation">{</span>
  <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="object-没啥用"><a href="#object-没啥用" class="header-anchor">#</a> object（没啥用）</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> obj<span class="token operator">:</span> object <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="array"><a href="#array" class="header-anchor">#</a> array</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> list<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> list<span class="token operator">:</span> <span class="token builtin">Array</span><span class="token operator">&lt;</span><span class="token builtin">number</span><span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="tuple"><a href="#tuple" class="header-anchor">#</a> tuple</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> x<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">number</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="enum"><a href="#enum" class="header-anchor">#</a> enum</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">enum</span> Color <span class="token punctuation">{</span>
  Red<span class="token punctuation">,</span>
  Green<span class="token punctuation">,</span>
  Blue<span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token keyword">let</span> c<span class="token operator">:</span> Color <span class="token operator">=</span> Color<span class="token punctuation">.</span>Green<span class="token punctuation">;</span>

<span class="token keyword">enum</span> Color <span class="token punctuation">{</span>
  Red <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
  Green<span class="token punctuation">,</span>
  Blue<span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token keyword">let</span> c<span class="token operator">:</span> Color <span class="token operator">=</span> Color<span class="token punctuation">.</span>Green<span class="token punctuation">;</span>

<span class="token keyword">enum</span> Color <span class="token punctuation">{</span>
  Red <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
  Green <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">,</span>
  Blue <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token keyword">let</span> c<span class="token operator">:</span> Color <span class="token operator">=</span> Color<span class="token punctuation">.</span>Green<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="类型断言"><a href="#类型断言" class="header-anchor">#</a> 类型断言</h3> <p>有些情况下，变量的类型对于我们来说是很明确，但是 TS 编译器却并不清楚，此时，可以通过类型断言来告诉编译器变量的类型，断言有两种形式：</p> <ul><li><p>第一种</p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> someValue<span class="token operator">:</span> <span class="token builtin">unknown</span> <span class="token operator">=</span> <span class="token string">&quot;this is a string&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> strLength<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token punctuation">(</span>someValue <span class="token keyword">as</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li> <li><p>第二种</p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> someValue<span class="token operator">:</span> <span class="token builtin">unknown</span> <span class="token operator">=</span> <span class="token string">&quot;this is a string&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> strLength<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">&lt;</span><span class="token builtin">string</span><span class="token operator">&gt;</span>someValue<span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul></div></div> <!----> <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">更新时间:</span> <span class="time">2020年12月23日星期三晚上11点26分</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/docs/pages/d2aa68/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">开发环境搭建</div></a> <a href="/docs/pages/565233/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">编译选项</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/docs/pages/d2aa68/" class="prev">开发环境搭建</a></span> <span class="next"><a href="/docs/pages/565233/">编译选项</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/docs/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/docs/pages/81236a/"><div>泛型 (Generic)</div></a> <span>12-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/docs/pages/ff09e5/"><div>接口 (Interface)</div></a> <span>12-23</span></dt></dl><dl><dd>03</dd> <dt><a href="/docs/pages/a2f5e6/"><div>面向对象的特点</div></a> <span>12-23</span></dt></dl> <dl><dd></dd> <dt><a href="/docs/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><!----> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> <!----></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">跟随系统</li><li class="iconfont icon-rijianmoshi">浅色模式</li><li class="iconfont icon-yejianmoshi">深色模式</li><li class="iconfont icon-yuedu">阅读模式</li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><!----></div></div>
    <script src="/docs/assets/js/app.9a1ea3d7.js" defer></script><script src="/docs/assets/js/2.b157a060.js" defer></script><script src="/docs/assets/js/9.1484fc35.js" defer></script>
  </body>
</html>